package com.chandra.pos.product.dao;

import com.chandra.pos.SearchCriteria;
import com.chandra.pos.product.dao.entity.Category;
import com.chandra.pos.product.dao.entity.Product;
import com.chandra.pos.product.exception.ProductNotFoundException;
import com.chandra.pos.product.repository.CategoryRepository;
import com.chandra.pos.product.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.inject.Named;
import java.util.List;

/**
 * 
 * Customer DAO
 * 
 * @author onlinetechvision.com
 * @since 25 Mar 2012
 * @version 1.0.0
 *
 */

@Named
public class CategoryDAO implements ICategoryDAO {



    @Autowired
    private CategoryRepository productRepository;
	/**
	 * Add Product
	 * 
	 * @param  Product product
	 */
	@Override
	public Category addProduct(Category product) {
       return productRepository.save(product);
	}

	/**
	 * Delete Product
	 * 
	 * @param  Product product
	 */
	@Override
	public void deleteProduct(Category product) {
        productRepository.delete(product);
	}

	/**
	 * Update Product
	 * 
	 * @param  Product product
	 */
	@Override
	public Category updateProduct(Category product) {
       return productRepository.save(product);
	}

	/**
	 * Get Product
	 * 
	 * @param  int Product Id
	 * @return Product 
	 */
	@Override
	public Category getProductById(Long id) {

       return productRepository.findOne(id);


	}

	/**
	 * Get Product List
	 * 
	 * @return List - Product list
	 */
	@Override
	public Page getProducts(PageRequest posPagination) {


       Page list = productRepository.findAll(posPagination);
		return list;
	}

    @Override
    /*
    http://docs.spring.io/spring-data/data-jpa/docs/1.0.x/reference/html/#repositories.custom-behaviour-for-all-repositories
     */
    public List<Category> searchProducts(String query) {
        return productRepository.findByNameLike(query);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Category getProduct(String name) throws ProductNotFoundException {
        Category product= productRepository.findByName(name);  //To change body of implemented methods use File | Settings | File Templates.

       if(product ==null)
           throw new ProductNotFoundException();

        return product;
    }



}
